﻿using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using WP7Client.Helpers;
using System.Windows;
using System;
using Microsoft.Phone.Controls;
using WP7Client.Managers;
using MSH.Common.Classes;
using WP7Client.ServerManager;
using MSH.Common.DAO.Results;
using WP7Client.UserInterface.Pages;
using WP7Client.Classes;

namespace WP7Client.Controller
{
    public class SettingController : BaseController
    {
        #region Members

        private int _GPSInterval;
        private int _GPSIntervalIndex = 0;
        private int _peopleNearbyRangeIndex = 0;
        private int _peopleNearbyRange;
        private PrivacyEnum locationPrivacy;
        private PrivacyEnum profilePrivacy;
        private MapVisibility mapVisibility;

        #endregion

        #region Get&Set

        public ICommand LogoutCommand
        {
            get;
            internal set;
        }

        public ICommand AboutCommand
        {
            get;
            internal set;
        }

        public ICommand SaveSettingsCommand
        {
            get;
            internal set;
        }

        public PrivacyEnum LocationPrivacy
        {
            get { return locationPrivacy; }
            set { locationPrivacy = value; }
        }

        public PrivacyEnum ProfilePrivacy
        {
            get { return profilePrivacy; }
            set { profilePrivacy = value; }
        }

        public int GPSInterval
        {
            get { return _GPSInterval; }
            set { _GPSInterval = value; }
        }

        public int GPSIntervalIndex
        {
            get { return _GPSIntervalIndex; }
            set
            {
                _GPSIntervalIndex = value;
                if (value != null)
                {
                    switch ((int)value)
                    {
                        case 0:
                            GPSInterval = 60000 * 5;
                            break;
                        case 1:
                            GPSInterval = 60000 * 10;
                            break;
                        case 2:
                            GPSInterval = 60000 * 15;
                            break;
                        case 3:
                            GPSInterval = 60000 * 30;
                            break;
                    }

                    OnPropertyChanged("GPSInterval");
                }
            }
        }

        public int PeopleNearbyRange
        {
            get { return _peopleNearbyRange; }
            set { _peopleNearbyRange = value; }
        }

        public int PeopleNearbyRangeIndex
        {
            get { return _peopleNearbyRangeIndex; }
            set
            {
                _peopleNearbyRangeIndex = value;
                if (value != null)
                {
                    switch ((int)value)
                    {
                        case 0:
                            PeopleNearbyRange = 500;
                            break;
                        case 1:
                            PeopleNearbyRange = 1000;
                            break;
                        case 2:
                            PeopleNearbyRange = 5000;
                            break;
                        case 3:
                            PeopleNearbyRange = 10000;
                            break;
                    }

                    OnPropertyChanged("PeopleNearbyRange");
                }
            }
        }

        public MapVisibility MapVisibility
        {
            get { return mapVisibility; }
            set { mapVisibility = value; }
        }

        #endregion

        public SettingController()
        {
            PeopleNearbyRangeIndex = 0;
            LogoutCommand = new RelayCommand(LogoutExecute);
            AboutCommand = new RelayCommand(AboutExecute);
            SaveSettingsCommand = new RelayCommand(SaveSettings);

            #region initData

            Object pprivacy = IsolatedStorageHelper.GetData(UserHelper.Username + ".ProfilePrivacy");
            if (pprivacy != null)
                ProfilePrivacy = (PrivacyEnum)pprivacy;
            else
                ProfilePrivacy = PrivacyEnum.Friends;

            Object locprivacy = IsolatedStorageHelper.GetData(UserHelper.Username + ".LocationPrivacy");
            if (locprivacy != null)
                LocationPrivacy = (PrivacyEnum)locprivacy;
            else
                LocationPrivacy = PrivacyEnum.Friends;

            Object mapVis = IsolatedStorageHelper.GetData(UserHelper.Username + ".MapVisibility");
            if (mapVis != null)
                MapVisibility = (MapVisibility)mapVis;
            else
                MapVisibility = MapVisibility.FriendsAndStrangers;

            Object gps = IsolatedStorageHelper.GetData(UserHelper.Username + ".GPSInterval");
            if (gps != null)
            {
                Int32 gpsInt = Convert.ToInt32(gps);
                switch (gpsInt)
                {
                    case (60000 * 5):
                        GPSIntervalIndex = 0;
                        break;
                    case (60000 * 10):
                        GPSIntervalIndex = 1;
                        break;
                    case (60000 * 15):
                        GPSIntervalIndex = 2;
                        break;
                    case (60000 * 30):
                        GPSIntervalIndex = 3;
                        break;
                    default:
                        GPSIntervalIndex = 0;
                        break;
                }
            }
            else
                GPSIntervalIndex = 0;


            Object nearbyRange = IsolatedStorageHelper.GetData(UserHelper.Username + ".PeopleNearbyRange");
            if (nearbyRange != null)
            {
                Int32 rangeInt = Convert.ToInt32(nearbyRange);
                switch (rangeInt)
                {
                    case 500:
                        PeopleNearbyRangeIndex = 0;
                        break;
                    case 1000:
                        PeopleNearbyRangeIndex = 1;
                        break;
                    case 5000:
                        PeopleNearbyRangeIndex = 2;
                        break;
                    case 10000:
                        PeopleNearbyRangeIndex = 3;
                        break;
                    default:
                        PeopleNearbyRangeIndex = 0;
                        break;
                }    
            
            }
            else
                PeopleNearbyRangeIndex = 0;

            #endregion
        }

        /// <summary>
        /// Uitloggen in MSH
        /// </summary>
        private void LogoutExecute()
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                MessageBoxResult result = MessageBox.Show("Are you sure you want to logout?", "Confirm", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    ControllerHelper.ConversationsController.Save();
                    IsolatedStorageHelper.DeleteData("token");
                    IsolatedStorageHelper.DeleteData("username");
                    ControllerHelper.Shutdown();
                    NavigationHelper.CanShutdown = true;
                    NavigationHelper.NavigateTo("/UserInterface/Pages/LoginPage.xaml");
                }
            });
        }

        /// <summary>
        /// Open de about page van MSH
        /// </summary>
        private void AboutExecute()
        {
            NavigationHelper.NavigateTo("/UserInterface/Pages/AboutPage.xaml");
        }

        /// <summary>
        /// Sla de instellingen op
        /// </summary>
        private void SaveSettings()
        {
            Loading = true;

            IsolatedStorageHelper.SetData(UserHelper.Username + ".LocationPrivacy", LocationPrivacy);
            IsolatedStorageHelper.SetData(UserHelper.Username + ".ProfilePrivacy", ProfilePrivacy);
            IsolatedStorageHelper.SetData(UserHelper.Username + ".GPSInterval", GPSInterval);
            IsolatedStorageHelper.SetData(UserHelper.Username + ".PeopleNearbyRange", PeopleNearbyRange);
            IsolatedStorageHelper.SetData(UserHelper.Username + ".MapVisibility", MapVisibility);

            LocationManager.ChangeTimerInterval(GPSInterval);

            HttpGet httpGetSaveSettings = new HttpGet(GlobalHelper.Url + String.Format("UpdateSettings/{0}/{1}/{2}", UserHelper.Token, LocationPrivacy.ToString(), ProfilePrivacy.ToString()), typeof(UpdateSettingsResult));
            httpGetSaveSettings.FinishedGet += SaveSettings_FinishedGet;
        }

        private void SaveSettings_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get != null && (get.resultObject as UpdateSettingsResult) != null)
                {
                    UpdateSettingsResult result = get.resultObject as UpdateSettingsResult;
                    if (result.TokenValidationResult == TokenValidationResultEnum.Valid) { }
                    else { }
                }
            }
            catch (Exception ex) { }
            finally { Loading = false; }
        }
    }
}
